﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace E2_WEBSITE.Code.Product
{
    public class ProductController
    {
        private ProductDataAccess m_ProductDAO = new ProductDataAccess();

        public bool AddFavoriteProduct(int idUser, int idSanPham)
        {
            try
            {
                if (!m_ProductDAO.CheckProductFavourite(idUser, idSanPham))
                {
                    return m_ProductDAO.AddFavoriteProduct(idUser, idSanPham);
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public String GetImageURLFromID(int id)
        {
            try
            {
                return m_ProductDAO.GetImageURLFromID(id);
            }
            catch
            {
                return "";
            }
        }

        public DataTable GetTableProductSaleOff()
        {
            try
            {
                return m_ProductDAO.GetTableProductSaleOff();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public ProductInfo GetTopLaptop()
        {
            try
            {
                return m_ProductDAO.GetTopLaptop();
            }
            catch
            {
                return null;
            }
        }

        public bool UpdateProduct(ProductInfo productInfo)
        {
            try
            {
                return m_ProductDAO.UpdateProduct(productInfo);
            }
            catch
            {
                return false;
            }
        }

        public bool InsertProduct(ProductInfo productInfo)
        {
            try
            {
                return m_ProductDAO.InsertProduct(productInfo);
            }
            catch
            {
                return false;
            }
        }

        public List<ProductInfo> getTopNewProduct(int numProduct)
        {
            try
            {
                return m_ProductDAO.getTopNewProduct(numProduct);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable getTableProductByCategory(int categoryId)
        {            
            try
            {
                return m_ProductDAO.getTableProductByCategory(categoryId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<ProductInfo> getTopFavoriteProduct(int numProduct)
        {
            try
            {
                return m_ProductDAO.getTopFavoriteProduct(numProduct);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public ProductInfo getProductDetail(string sProductId, string sCategory, out DataTable dtSpecificInformation)
        {
            try
            {                
                return m_ProductDAO.getProductDetail(sProductId, sCategory, out dtSpecificInformation);                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool DeleteProductWithID(int id)
        {
            try
            {
                return m_ProductDAO.DeleteProductWithID(id);
            }
            catch
            {
                return false;
            }
        }

        public ProductInfo getProductById(int idProduct)
        {
            try
            {
                return m_ProductDAO.getProductById(idProduct);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<string> GetListProductNameByManufactureNameAndCatelogyName(string sManufactureName,string sCatelogyName)
        {
            try
            {
                return m_ProductDAO.GetListProductNameByMaFeNameAndCatName(sManufactureName,sCatelogyName);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public int GetProductIdByProductNameAndCatelogyAndManufacture(string sProduct,string sManufacture,string sCatelogy)
        {
            try
            {
                return m_ProductDAO.GetProductIDByProductNameAndCateLogyAndManufacture(sProduct, sManufacture, sCatelogy);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public List<LoaiSanPham> GetLoaiSanPhamList()
        {
            try
            {
                return m_ProductDAO.GetLoaiSanPhamList();
            }
            catch
            {
                return null;
            }
        }

        public List<NhaSanXuat> GetNhaSanXuatList()
        {
            try
            {
                return m_ProductDAO.GetNhaSanXuatList();
            }
            catch
            {
                return null;
            }
        }

        public List<KhuyenMai> GetKhuyenMaiList()
        {
            try
            {
                return m_ProductDAO.GetKhuyenMaiList();
            }
            catch
            {
                return null;
            }
        }

        public List<NhaPhanPhoi> GetNhaPhanPhoiList()
        {
            try
            {
                return m_ProductDAO.GetNhaPhanPhoiList();
            }
            catch
            {
                return null;
            }
        }

        public DataTable GetSanPhamList()
        {
            try
            {
                return m_ProductDAO.GetSanPhamList();
            }
            catch
            {
                return null;
            }
        }

        public DataTable GetSanPhamListWithTenSanPham(String strTenSanPham)
        {
            try
            {
                return m_ProductDAO.GetSanPhamListWithTenSanPham(strTenSanPham);
            }
            catch
            {
                return null;
            }
        }


        public DataTable GetSanPhamListWithLoaiSanPham(String strLoaiSanPham)
        {
            List<LoaiSanPham> lspList = m_ProductDAO.GetLoaiSanPhamList();
            int id = -1;
            for (int i = 0; i < lspList.Count; i++)
            {
                if (lspList[i].TenLoaiSanPham == strLoaiSanPham)
                {
                    id = lspList[i].ID;
                    break;
                }
            }

            if (id == -1)
                return null;
            else
            {
                try
                {
                    return m_ProductDAO.GetSanPhamListWithLoaiSanPham(id);
                }
                catch
                {
                    return null;
                }
            }
        }

        public DataTable GetSanPhamListWithNhaSanXuat(String strNSX)
        {
            List<NhaSanXuat> nsxList = m_ProductDAO.GetNhaSanXuatList();
            int id = -1;
            for (int i = 0; i < nsxList.Count; i++)
            {
                if (nsxList[i].TenNSX == strNSX)
                {
                    id = nsxList[i].ID;
                    break;
                }
            }

            if (id == -1)
                return null;
            else
            {
                try
                {
                    return m_ProductDAO.GetSanPhamListWithNhaSanXuat(id);
                }
                catch
                {
                    return null;
                }
            }
        }

        public DataTable GetSanPhamListWithNhaPhanPhoi(String strNPP)
        {
            List<NhaPhanPhoi> nppList = m_ProductDAO.GetNhaPhanPhoiList();
            int id = -1;
            for (int i = 0; i < nppList.Count; i++)
            {
                if (nppList[i].TenNhanPhanPhoi == strNPP)
                {
                    id = nppList[i].ID;
                    break;
                }
            }

            if (id == -1)
                return null;
            else
            {
                try
                {
                    return m_ProductDAO.GetSanPhamListWithNhaPhanPhoi(id);
                }
                catch
                {
                    return null;
                }
            }
        }

        internal List<ProductInfo> getTopBestSellProduct(int p)
        {
            try
            {
                return m_ProductDAO.getTopBestSellProduct(p);
            }
            catch
            {
                return null;
            }
        }
    }
}
